package com.xxx.video.commen.utils;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.download.DownloadTask;
import com.xxx.video.download.SpeedCalculator;
import com.xxx.video.download.core.Util;
import com.xxx.video.download.core.breakpoint.BlockInfo;
import com.xxx.video.download.core.breakpoint.BreakpointInfo;
import com.xxx.video.download.core.cause.EndCause;
import com.xxx.video.download.core.listener.DownloadListener4WithSpeed;
import com.xxx.video.download.core.listener.assist.Listener4SpeedAssistExtend;

import java.util.List;
import java.util.Map;

public class RecyclerviewDownloadListener extends DownloadListener4WithSpeed {
    private long totalLength;
    private String readableTotalLength;
    private BaseViewHolder baseViewHolder;

    public RecyclerviewDownloadListener(BaseViewHolder baseViewHolder) {
        this.baseViewHolder = baseViewHolder;
    }

    public BaseViewHolder getBaseViewHolder() {
        return baseViewHolder;
    }

    public void setBaseViewHolder(BaseViewHolder baseViewHolder) {
        this.baseViewHolder = baseViewHolder;
    }

    @Override
    public void taskStart(@NonNull DownloadTask task) {

    }

    @Override
    public void connectStart(@NonNull DownloadTask task, int blockIndex, @NonNull Map<String, List<String>> requestHeaderFields) {

    }

    @Override
    public void connectEnd(@NonNull DownloadTask task, int blockIndex, int responseCode, @NonNull Map<String, List<String>> responseHeaderFields) {

    }

    @Override
    public void infoReady(@NonNull DownloadTask task, @NonNull BreakpointInfo info, boolean fromBreakpoint, @NonNull Listener4SpeedAssistExtend.Listener4SpeedModel model) {
        totalLength = info.getTotalLength();
        readableTotalLength = Util.humanReadableBytes(totalLength, true);
        L.i("download", "【2、infoReady】current progress" + (float) info.getTotalOffset() / totalLength * 100 + "%" + "，" + info.toString());
    }

    @Override
    public void progressBlock(@NonNull DownloadTask task, int blockIndex, long currentBlockOffset, @NonNull SpeedCalculator blockSpeed) {

    }

    @Override
    public void progress(@NonNull DownloadTask task, long currentOffset, @NonNull SpeedCalculator taskSpeed) {
        String readableOffset = Util.humanReadableBytes(currentOffset, true);
        String progressStatus = readableOffset + "/" + readableTotalLength;
        String speed = taskSpeed.speed();
        float percent = (float) currentOffset / totalLength * 100;
        L.i("download", "【6、progress】" + currentOffset + "[" + progressStatus + "]，speed：" + speed + "，progress：" + percent + "%");
        if (baseViewHolder != null && task.getUrl().equals(baseViewHolder.getTag())) {
            baseViewHolder.refresh(((int) percent));
        }
    }

    @Override
    public void blockEnd(@NonNull DownloadTask task, int blockIndex, BlockInfo info, @NonNull SpeedCalculator blockSpeed) {

    }

    @Override
    public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause, @Nullable Exception realCause, @NonNull SpeedCalculator taskSpeed) {
        try {
            String exceptionMsg = realCause == null ? "" : realCause.toString();
            L.i("download", "【end】" + cause.toString() + "***" + exceptionMsg);
            if (!TextUtils.isEmpty(exceptionMsg)) {
                if (!exceptionMsg.contains("The current offset on block-info isn't update correct")) {
                    if (baseViewHolder != null && task.getUrl().equals(baseViewHolder.getTag())) {
                        baseViewHolder.setStatus(task.getUrl());
                    }
                } else {
                    DownloadUtils.getInstance().download(task.getUrl(),this);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
